
{extend name="public/base"/}

{block name="content"}

<div class="layui-card">
    <div class="layui-card-header">插件管理</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="table" lay-filter="table"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
    <a class="layui-btn layui-btn-sm" id="local-install">本地安装</a>
  </div>
</script>
<script type="text/html" id="titleTpl">
    {{#  if(d.uninstall == "" || d.uninstall == null || d.uninstall == undefined){ }}
    {{#  if(d.configl == "" || d.configl == null || d.configl == undefined){ }}
    <a class="layui-btn layui-btn-xs" lay-event="config" >设置</a>
    {{#  } }}
    {{#  if(d.status == 0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal " lay-event="enable" >启用</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-xs layui-btn-warm "  lay-event="disable" >禁用</a>
    {{#  } }}
    <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="uninstall" >卸载</a>
{{#  } else { }}
    <a class="layui-btn layui-btn-xs " lay-event="install" >安装</a>
{{#  } }}
</script>
{/block}

{block name="js"}
<script>
layui.use(['table', 'upload','lucky'], function() {
    let table = layui.table,
        $ = layui.$,
        lucky=layui.lucky,
        upload = layui.upload;
        table.render({
        elem: '#table',
        toolbar: '#toolbarDemo',
        url: '{:url("index")}',
        id:'table_id',
        cols: [
            [
                { field: 'title', width: 150, title: '名称' },
                { field: 'name', width: 150, title: '标识' },
                { field: 'description', title: '描述' },
                { field: 'status_text', width: 80, title: '状态', templet: '<div>{{#  if(d.status_text){ }} {{d.status_text}} {{#  } else { }} 未安装 {{#  } }}</div>' },
                { field: 'author', width: 90, title: '作者' },
                { field: 'version', width: 90, title: '版本' },
                { fixed: 'right', width: 180, title: '操作', templet: '#titleTpl' }
            ]
        ]
    });


    /**
     * 监听单行工具操作
     */
    table.on('tool(table)', function (obj) {
        let data = obj.data;
        let _id=parseInt(data.id);
        let layEvent = obj.event;
        if(layEvent==="enable"){
            lucky.Form_Confirm("{:url('addons/enable')}",{id:_id},"确定启用该插件吗？","POST","table_id");
        }else if(layEvent==="disable"){
            lucky.Form_Confirm("{:url('addons/disable')}",{id:_id},"确定禁止该插件吗？","POST","table_id");
        }else if(layEvent==="uninstall"){
            lucky.Form_Confirm("{:url('addons/uninstall')}",{id:_id},"确定卸载该插件吗？","GET","table_id");
        }
        else if(layEvent==="install"){
            lucky.Form_Confirm("{:url('addons/install')}",{addon_name:data.name},"确定安装该插件吗？","GET","table_id");
        } else if(layEvent==="config"){
            let urls="/addons/addons/config/id/"+_id;
            lucky.CreateForm("设置插件",'50%','60%',urls,"table_id",0);
        }
    });

    //执行实例
    let uploadInst = upload.render({
        elem: '#local-install',
        url: '{:url("addons/addons/local")}',
        accept: 'file',
        exts: 'zip',
        done: function(res) {
            //上传完毕回调
            layer.alert(res.msg, {}, function() {
                if (res.code != 0) {
                    location.reload();
                }
            });
        },
        error: function() {
            //请求异常回调
        }
    });
});
</script>
{/block}
